{{- define "nginx_resources" }}
cpu: 25m
memory: 50Mi
{{- end }}

{{- if .Values.flantIntegration.internal.madisonAuthKey }}
  {{- range $.Values.flantIntegration.internal.madison.backends }}
{{- if ($.Values.global.enabledModules | has "vertical-pod-autoscaler-crd") }}
---
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: madison-proxy-{{ . | sha256sum }}
  namespace: d8-monitoring
  {{- include "helm_lib_module_labels" (list $ (dict "app" "madison-proxy" "madison-backend" .)) | nindent 2 }}
spec:
  targetRef:
    apiVersion: "apps/v1"
    kind: Deployment
    name: madison-proxy-{{ . | sha256sum }}
  updatePolicy:
    updateMode: "Auto"
  resourcePolicy:
    containerPolicies:
    - containerName: nginx
      minAllowed:
        {{- include "nginx_resources" . | nindent 8 }}
      maxAllowed:
        cpu: 50m
        memory: 100Mi
{{- end }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: madison-proxy-{{ . | sha256sum }}
  namespace: d8-monitoring
  {{- include "helm_lib_module_labels" (list $ (dict "app" "madison-proxy" "madison-backend" .)) | nindent 2 }}
spec:
  replicas: 1
  revisionHistoryLimit: 2
  strategy:
    type: Recreate
  selector:
    matchLabels:
      app: madison-proxy
      madison-backend: {{ . }}
  template:
    metadata:
      labels:
        app: madison-proxy
        madison-backend: {{ . }}
      annotations:
        checksum/key: {{ include (print $.Template.BasePath "/madison/secret.yaml") $ | sha256sum }}
    spec:
      {{- include "helm_lib_node_selector" (tuple $ "monitoring") | nindent 6 }}
      {{- include "helm_lib_tolerations" (tuple $ "monitoring") | nindent 6 }}
      {{- include "helm_lib_priority_class" (tuple $ "cluster-medium") | nindent 6 }}
      {{- include "helm_lib_module_pod_security_context_run_as_user_nobody" $ | nindent 6 }}
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchLabels:
                  app: madison-proxy
              topologyKey: kubernetes.io/hostname
      containers:
      - name: nginx
        {{- include "helm_lib_module_container_security_context_read_only_root_filesystem" $ | nindent 8 }}
        image: {{ include "helm_lib_module_image" (list $ "madisonProxy") }}
        env:
        - name: MADISON_BACKEND
          value: {{ printf "%s:443" . }}
        - name: MADISON_AUTH_KEY
          valueFrom:
            secretKeyRef:
              name: madison-proxy
              key: auth-key
        {{- include "helm_lib_envs_for_proxy" $ | nindent 8 }}
        ports:
        - containerPort: 8080
          name: http
          protocol: TCP
        livenessProbe:
          httpGet:
            path: /healthz
            port: 8080
            scheme: HTTP
          initialDelaySeconds: 10
          timeoutSeconds: 5
        readinessProbe:
          httpGet:
            path: /readyz
            port: 8080
            scheme: HTTP
          timeoutSeconds: 5
        volumeMounts:
          - mountPath: /tmp
            name: tmp
        resources:
          requests:
            {{- include "helm_lib_module_ephemeral_storage_logs_with_extra" 10 | nindent 12 }}
  {{- if not ( $.Values.global.enabledModules | has "vertical-pod-autoscaler-crd") }}
            {{- include "nginx_resources" . | nindent 12 }}
  {{- end }}
      volumes:
        - name: tmp
          emptyDir: {}
      imagePullSecrets:
      - name: deckhouse-registry
  {{- end }}
{{- end }}
